package service

import (
	"context"
	__ "order_srv/basic/orderproto"
	"order_srv/handler/dao"
)

// Rating 统一评价接口实现
// 同时支持司机评价乘客和乘客评价司机
func (s *Server) Rating(_ context.Context, in *__.RatingReq) (*__.RatingResp, error) {
	id, err := dao.CreateRating(in)
	if err != nil {
		return nil, err
	}
	return &__.RatingResp{Id: id}, nil
}

// GetRatingsByTargetId 获取用户收到的评价列表
func (s *Server) GetRatingsByTargetId(_ context.Context, in *__.GetRatingsByTargetIdReq) (*__.GetRatingsByTargetIdResp, error) {
	ratings, err := dao.GetRatingsByTargetId(in.TargetId, int(in.RatingType))
	if err != nil {
		return nil, err
	}
	return &__.GetRatingsByTargetIdResp{Ratings: ratings}, nil
}

// GetAverageRating 获取用户平均评分
func (s *Server) GetAverageRating(_ context.Context, in *__.GetAverageRatingReq) (*__.GetAverageRatingResp, error) {
	averageRating, err := dao.GetAverageRating(in.TargetId, int(in.RatingType))
	if err != nil {
		return nil, err
	}
	return &__.GetAverageRatingResp{AverageRating: averageRating}, nil
}

// GetRatingByOrder 获取指定订单的评价
func (s *Server) GetRatingByOrder(_ context.Context, in *__.GetRatingByOrderReq) (*__.GetRatingByOrderResp, error) {
	rating, err := dao.GetRatingByOrder(in.OrderId, int(in.RatingType))
	if err != nil {
		return nil, err
	}
	return &__.GetRatingByOrderResp{Rating: rating}, nil
}